/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.summary.service.impl;

import me.zhengjie.modules.summary.domain.RecordSub;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.summary.repository.RecordSubRepository;
import me.zhengjie.modules.summary.service.RecordSubService;
import me.zhengjie.modules.summary.service.dto.RecordSubDto;
import me.zhengjie.modules.summary.service.dto.RecordSubQueryCriteria;
import me.zhengjie.modules.summary.service.mapstruct.RecordSubMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2022-12-26
**/
@Service
@RequiredArgsConstructor
public class RecordSubServiceImpl implements RecordSubService {

    private final RecordSubRepository recordSubRepository;
    private final RecordSubMapper recordSubMapper;

    @Override
    public Map<String,Object> queryAll(RecordSubQueryCriteria criteria, Pageable pageable){
        Page<RecordSub> page = recordSubRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recordSubMapper::toDto));
    }

    @Override
    public List<RecordSubDto> queryAll(RecordSubQueryCriteria criteria){
        return recordSubMapper.toDto(recordSubRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecordSubDto findById(String id) {
        RecordSub recordSub = recordSubRepository.findById(id).orElseGet(RecordSub::new);
        ValidationUtil.isNull(recordSub.getId(),"RecordSub","id",id);
        return recordSubMapper.toDto(recordSub);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecordSubDto create(RecordSub resources) {
        resources.setId(IdUtil.simpleUUID());
        return recordSubMapper.toDto(recordSubRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecordSub resources) {
        RecordSub recordSub = recordSubRepository.findById(resources.getId()).orElseGet(RecordSub::new);
        ValidationUtil.isNull( recordSub.getId(),"RecordSub","id",resources.getId());
        recordSub.copy(resources);
        recordSubRepository.save(recordSub);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            recordSubRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecordSubDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecordSubDto recordSub : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", recordSub.getName());
            map.put("关联记录表id", recordSub.getRecordId());
            map.put("创建时间", recordSub.getCreateTime());
            map.put("更新时间", recordSub.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}